﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.NewsManage
{
    public partial class NewsEdit : SecurityPage
    {
        public override string PrivCode()
        {
            return "dfc20dd7b315784e";
        }

        private void DropBanbing()
        {
            List<TypeInfo> list = TypeInfo.Where("ProjectTypeid=@0 and IsDel=@1", Convert.ToInt32(EnumData.ProjectType.新闻分类),false).ToList();
            this.Drop_TypeList.DataSource = list;
            this.Drop_TypeList.DataValueField = "id";
            this.Drop_TypeList.DataTextField = "TypeName";
            this.Drop_TypeList.DataBind();

            List<xy_bmxbm> xylist = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            xylist.Add(new xy_bmxbm() { BMDM = "0", BMMC = "全校" });
            this.Drop_xy.DataSource = xylist.OrderBy(p => p.BMDM).ToList();
            this.Drop_xy.DataValueField = "BMDM";
            this.Drop_xy.DataTextField = "BMMC";
            this.Drop_xy.DataBind();


        }

        private void LoadData()
        {
            string EditId = Request.QueryString["EditId"].ToString();
            Data.News md = Data.News.Where("id=@0",int.Parse(EditId)).First();
            this.txt_title.Text = md.Title;
            this.txt_Source.Text = md.Source;
            this.txt_Author.Text = md.Author;
            this.txt_ReleaseDate.Text = Convert.ToDateTime(md.ReleaseDate).ToString("yyyy-MM-dd");
            this.FCKeditor1.Value = md.Contents;
            this.Drop_xy.SelectedValue = md.XYDM;
            this.Drop_TypeList.SelectedValue = md.Typeid.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.DropBanbing();

            UserList user = UserList.Where("UserName=@0", User.Identity.Name).First();
            this.txt_Author.Text = user.UName;
            this.txt_ReleaseDate.Text = DateTime.Now.ToShortDateString();
            if (Convert.ToBoolean(user.IsStudent))
            {
                xy_xsxx info = xy_xsxx.Where("XH=@0", user.XH).FirstOrDefault();
                xydm xy = xydm.Where("XYMC=@0", info.XY).FirstOrDefault();
                if (xy != null)
                {
                    this.Drop_xy.SelectedValue = xy.XYDM;
                }
            }
            else
            {
                xy_jgxx info = xy_jgxx.Where("ZGH=@0",user.XH).FirstOrDefault();
                xydm xy = xydm.Where("XYMC=@0", info.XYMC).FirstOrDefault();
                if (xy != null)
                {
                    this.Drop_xy.SelectedValue = xy.XYDM;
                }
            }


            if (Request.QueryString["EditId"] != null)
                this.LoadData();
        }

        protected void Btn_Save_Click(object sender, EventArgs e)
        {
            UserList user = UserList.Where("UserName=@0", User.Identity.Name).First();

            Data.News md;
            if (Request.QueryString["EditId"] == null)
            {
                md=new Data.News();
            }
            else
            {
                md = Data.News.Where("id=@0", int.Parse(Request.QueryString["EditId"].ToString())).First();
            }
                
            md.Typeid = int.Parse(this.Drop_TypeList.SelectedValue);
            md.Userid = user.id;
            md.Title = this.txt_title.Text.Trim();
            md.Source = this.txt_Source.Text.Trim();
            md.Author = this.txt_Author.Text.Trim();
            md.ReleaseDate = Convert.ToDateTime(this.txt_ReleaseDate.Text.Trim());
            md.Contents = this.FCKeditor1.Value;
            md.XYDM = this.Drop_xy.SelectedValue;
            md.AddTime = DateTime.Now;
            if (Request.QueryString["EditId"] == null)
            {
                Data.News.Insert(md);
            }
            else
            {
                Data.News.Update(md, "id=@0", int.Parse(Request.QueryString["EditId"].ToString()));
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');window.location.href='NewsList.aspx';</script>");
        }

        protected void Btn_Return_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx");
        }
    }
}